package com.xingchen.feima.star.controller;


import com.xingchen.feima.common.utils.R;
import com.xingchen.feima.star.entity.NuclearPowerPlant;
import com.xingchen.feima.star.entity.ShippingTime;
import com.xingchen.feima.star.service.NuclearPowerPlantService;
import com.xingchen.feima.star.service.ShippingTimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 海运时间表 前端控制器
 * </p>
 *
 * @author yangxc
 * @since 2025-02-11
 */
@Api(tags = "海运时间管理")
@RestController
@RequestMapping("/star/shippingTime")
public class ShippingTimeController {

    @Autowired
    private ShippingTimeService shippingTimeService;

    @Autowired
    private NuclearPowerPlantService nuclearPowerPlantService;


    @ApiOperation(value = "保存海运时间矩阵")
    @PostMapping("/saveShippingTimeMatrix")
    public R saveShippingTimeMatrix(
            @ApiParam(name = "shippingTimeMatrix", value = "海运时间矩阵", required = true)
            @RequestBody List<List<ShippingTime>> shippingTimeMatrix){
        List<ShippingTime> shippingTimeListForSaveOrUpdate = new ArrayList<>();
        for (List<ShippingTime> shippingTimeRow : shippingTimeMatrix) {
            shippingTimeListForSaveOrUpdate.addAll(shippingTimeRow);
        }
        shippingTimeService.saveOrUpdateBatch(shippingTimeListForSaveOrUpdate);
        return R.ok();
    }

    @ApiOperation(value = "获取海运时间矩阵和核电站列表的Map")
    @GetMapping("/getShippingTimeMatrixAndNuclearPowerPlantListMap")
    public R getShippingTimeMatrixAndNuclearPowerPlantListMap(){
        List<ShippingTime> shippingTimeList = shippingTimeService.list(null);
        List<NuclearPowerPlant> nuclearPowerPlantList = nuclearPowerPlantService.list(null);
        List<List<ShippingTime>> shippingTimeMatrix = new ArrayList<>();
        for (NuclearPowerPlant outerNuclearPowerPlant : nuclearPowerPlantList) {
            ArrayList<ShippingTime> shippingTimeRow = new ArrayList<>();
            for (NuclearPowerPlant innerNuclearPowerPlant : nuclearPowerPlantList) {
                ShippingTime shippingTimeInit = new ShippingTime();
                shippingTimeInit.setShippingTimeValue(0D);
                shippingTimeInit.setStartPointId(outerNuclearPowerPlant.getId());
                shippingTimeInit.setEndPointId(innerNuclearPowerPlant.getId());
                for (ShippingTime shippingTime : shippingTimeList) {
                    if (outerNuclearPowerPlant.getId().equals(shippingTime.getStartPointId()) && innerNuclearPowerPlant.getId().equals(shippingTime.getEndPointId())) {
                        shippingTimeInit = shippingTime;
                        break;
                    }
                }
                shippingTimeRow.add(shippingTimeInit);
            }
            shippingTimeMatrix.add(shippingTimeRow);
        }

        Map<String, Object> shippingTimeMatrixAndNuclearPowerPlantListMap = new HashMap<>();
        shippingTimeMatrixAndNuclearPowerPlantListMap.put("shippingTimeMatrix", shippingTimeMatrix);
        shippingTimeMatrixAndNuclearPowerPlantListMap.put("nuclearPowerPlantList", nuclearPowerPlantList);

        return R.ok().data("items", shippingTimeMatrixAndNuclearPowerPlantListMap);
    }
}

